package cn.liyicun.cp.cpweb.controller.cp;

import cn.hutool.json.JSONObject;
import cn.liyicun.cp.cpweb.base.Result;
import cn.liyicun.cp.cpweb.controller.BaseController;
import cn.liyicun.cp.cpweb.entity.ExpectInfo;
import cn.liyicun.cp.cpweb.entity.GetApicp;
import cn.liyicun.cp.cpweb.service.ExpectInfoService;
import cn.liyicun.cp.cpweb.service.cp.GetApicpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = {"/bjpk10"})
public class BJPK10Controller extends BaseController {

    @Autowired
    private GetApicpService getApicpService;
    @Autowired
    ExpectInfoService service;


    //bjsc首页
    @RequestMapping(value = {"/", "index"})
    public String index(HttpServletRequest request, Map<String, Object> map) {
        Long uid = getLoginByIdError(request);

        return "bjpk10/index";
    }


    //提现
    @RequestMapping(value = "/history_list")
    public String withdraw(HttpServletRequest request, Map<String, Object> map, @RequestParam(value = "page", defaultValue = "1") int page) {
        Long uid = getLoginByIdError(request);
        int limit = 100;
        String code = "bjpk10";
        List<GetApicp> lists = getApicpService.list(code, page, limit);
        map.put("lists", lists);
        return "bjpk10/history_list";
    }

    //将要开奖
    @RequestMapping(value = "/next_expect")
    @ResponseBody
    public Result<?> nextExpect(HttpServletRequest request) {
        Long uid = getLoginByIdError(request);
        ExpectInfo data = service.nextExpect();

        JSONObject json = new JSONObject();
        json.put("expect", data.getExpect());
        json.put("endTime", data.getEndTime());
        json.put("endLong", (data.getEndTime().getTime() - System.currentTimeMillis()) / 1000L);

        return Result.successData(json);
    }


    //最近开奖的期数
    @RequestMapping(value = "/list_lately_expect")
    public String listLatelyExpect(HttpServletRequest request, Map<String, Object> map) {
        Long uid = getLoginByIdError(request);
        List<ExpectInfo> latelyExpects = service.listLatelyExpect(10);
        map.put("latelyExpects", latelyExpects);
        return"bjpk10/list_lately_expect";
    }

    //最近开奖的期数
    @RequestMapping(value = "/get_expect_info")
    @ResponseBody
    public Result<?> getExpectInfo(HttpServletRequest request, @RequestParam(value = "expect", required = true) Long expect
            , @RequestParam(value = "code", required = true) String code) {
        Long uid = getLoginByIdError(request);
        ExpectInfo expectInfo = service.getExpectInfo(code, expect);
        return Result.successData(expectInfo.getOpenStatus());
    }


}
